package logging;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

// this is the logger class that writes messages into a file using a time stamp.
public class Logging {

	private static FileWriter fstream = null;
	private static BufferedWriter out = null;
	private static FileWriter fstream2 = null;
	private static BufferedWriter out2 = null;
	private static FileWriter fstreamQueue = null;
	private static BufferedWriter outQueue = null;

	private static long startTime;
	
	public static void closeWriter() {
		try {
			out.close();
			fstream.close();
			out2.close();
			fstream2.close();
			if (outQueue != null) {
				outQueue.close();
				fstreamQueue.close();
			}
		} catch (IOException e) {
			System.err.println("Closing Stream/File Writer Error: " + e.getMessage());
		}
	}
	
	// opens the writer for a client
	public static void openClientWriter() {
		startTime = Calendar.getInstance().getTimeInMillis();
		try{
			// long log file
			fstream = new FileWriter("clientLog" + Long.toString(startTime) + "_1.txt");
			out = new BufferedWriter(fstream);
			// short log file used for creating graphs
			fstream2 = new FileWriter("clientLog" + Long.toString(startTime) + "_2.txt");
			out2 = new BufferedWriter(fstream2);
			out.write("TAG");
			out.write("\t");
			out.write("EVENT_DESC");
			out.write("\t");
			out.write("TSTAMP");
			out.write("\t");
			out.write("DATA");
			out.newLine();
		}catch (Exception e){
			System.err.println("Creating Client Stream/File Writer Error: " + e.getMessage());
		}
	}
	
	// opens the writer for a middleware
	public static void openMiddlewareWriter() {
		startTime = Calendar.getInstance().getTimeInMillis();
		try{
			// long log file
			fstream = new FileWriter("middlewareLog" + Long.toString(startTime) + "_1.txt");
			out = new BufferedWriter(fstream);
			// short log file used for creating graphs
			fstream2 = new FileWriter("middlewareLog" + Long.toString(startTime) + "_2.txt");
			out2 = new BufferedWriter(fstream2);
			// queue size log
			fstreamQueue = new FileWriter("queueLog" + Long.toString(startTime) + ".txt");
			outQueue = new BufferedWriter(fstreamQueue);
			out.write("TAG");
			out.write("\t");
			out.write("EVENT_DESC");
			out.write("\t\t");
			out.write("TSTAMP");
			out.write("\t");
			out.write("DATA");
			out.newLine();
		}catch (Exception e){
			System.err.println("Creating Middleware Stream/File Writer Error: " + e.getMessage());
		}
	}
	
	// log the 4-tuple  <eventCode, eventDescription, timestamp, eventData>
	public static void logEvent(Integer eventCode, String eventDescription, String eventData) {
		Long relativeTime = Calendar.getInstance().getTimeInMillis() - startTime;
		try {
			out.write(eventCode.toString());
			out.write("\t");
			out.write(eventDescription);
			out.write("\t");
			out.write(Long.toString(relativeTime));
			out.write("\t");
			out.write(eventData);
			out.newLine();
			out2.write(eventCode.toString());
			out2.write("\t");
			// out2.write(eventDescription);
			//out2.write("\t");
			out2.write(Long.toString(relativeTime));
			out2.newLine();		
		} catch (IOException e) {
			System.err.println("logEvent() -> Error Writing to BufferedWriter:" + e.getMessage());
		}

	}
	
	public static void logQueueSize(Integer queueSize) {
		try {
			outQueue.write(queueSize.toString());
			outQueue.newLine();
		} catch (IOException e) {
			System.err.println("logQueueSize() -> Error Writing to BufferedWriter:" + e.getMessage());
		}
	}
}
